package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.SysConfigService;
import com.itheima.ydd.vo.ChildrenVo;
import com.itheima.ydd.vo.SysConfigVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    /**
     * 权限管理系统设置,查询所有配置数据
     *
     * @return
     */
    @Override
    public Map<String, Object> all() {

        //封装list数据:
        //先根据tab_value分组
        LambdaQueryWrapper<SysConfig> tableValueLqw = new LambdaQueryWrapper<>();
        tableValueLqw.groupBy(SysConfig::getTab_value);
        List<SysConfig> sysConfigs = this.list(tableValueLqw);

        List<SysConfigVo> list = new ArrayList<>();

        //Map<String, Object> children = new HashMap<>();

        for (SysConfig sysConfig : sysConfigs) {
            //判断本次循环是属于哪个组件的内容
            String tab_value = sysConfig.getTab_value();
            SysConfigVo sysConfigVo = new SysConfigVo();

            if ("base".equals(tab_value)) {
                sysConfigVo.setName("基础配置");
            } else if ("custom".equals(tab_value)) {
                sysConfigVo.setName("其他设置");
            } else {
                sysConfigVo.setName("系统设置");
            }
            sysConfigVo.setKey(tab_value);

            //再根据group_value分组
            LambdaQueryWrapper<SysConfig> slqw = new LambdaQueryWrapper<>();
            slqw.groupBy(SysConfig::getGroup_value);
            List<SysConfig> list1 = this.list(slqw);

            List<ChildrenVo> childrenVos = new ArrayList<>();
            for (SysConfig config : list1) {
                ChildrenVo childrenVo = new ChildrenVo();
                //处理一下children的name属性
                String group_value = config.getGroup_value();
                childrenVo.setName(group_value);
                //根据group_Value查询所有
                LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(SysConfig::getGroup_value, config.getGroup_value());
                List<SysConfig> configs = this.list(lambdaQueryWrapper);
                childrenVo.setData(configs);
                childrenVos.add(childrenVo);
            }

            sysConfigVo.setChildren(childrenVos);
            list.add(sysConfigVo);
        }
        //封装allDate数据
        List<SysConfig> allData = this.list();

        //封装configForm和configRules数据
       Map<String,String> configRules=new HashMap<>();
        Map<String, String> configForm = new HashMap<>();
        for (SysConfig config : allData) {
            configForm.put(config.getAttr_key(), config.getAttr_value());
            if (config.getAttr_key().equals("site_name")) {
                configRules.put("site_name",config.getValidator());
            }
        }

        Map<String, Object> all = new HashMap<>();
        all.put("list", list);
        all.put("allData", allData);
        all.put("configForm", configForm);
        all.put("configRules",configRules);
        return all;
    }
}
